﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using SoftwareDiscipline.SharePoint.UserManagement.Data;

namespace SoftwareDiscipline.SharePoint.UserManagement.Layouts.SoftwareDiscipline
{
    public partial class EditRoles : LayoutsPageBase
    {
        private string selectedRole = String.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (selectedRole != null)
                {
                    textRoleName.Text = this.SelectedRole;
                    LitOroginalRole.Text = this.SelectedRole;
                }
            }

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            AllRolesDataSource allRolesDataSource = new AllRolesDataSource();
            string status = allRolesDataSource.EditRole(LitOroginalRole.Text, textRoleName.Text);

            if (status == "FAIL")
            {
                this.LabelErrorMessage.Text = this.GetGlobalResourceObject("swusrmgmt", "EditRoles_GenericError") as string;
            }
            if (status == "SUCCESS")
            {
                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                Context.Response.Flush();
                Context.Response.End();
            }
            else
            {
                this.LabelErrorMessage.Text = status;
            }
        }

        /// <summary>
        /// This Getter will retrives the Selected Rolename frol AllUsers.aspx 
        /// </summary>
        protected string SelectedRole
        {
            get
            {
                if (this.selectedRole == string.Empty)
                {
                    string keyParam = this.Page.Request.QueryString["RoleName"];
                    if (!String.IsNullOrEmpty(keyParam))
                    {
                        this.selectedRole = keyParam;
                    }
                }

                return this.selectedRole;
            }
        }
    }
}
